import Cookies from 'js-cookie';

const TokenKey = 'hrsaas-ihrm-token';
const TimestampKey = 'hrsaas-ihrm-timestamp';
const loginTimeout = 1000 * 60 * 60; // 单位毫秒

export function getToken() {
  return Cookies.get(TokenKey);
}

export function setToken(token) {
  return Cookies.set(TokenKey, token);
}

export function removeToken() {
  return Cookies.remove(TokenKey);
}

/**
 * 设置登录时间戳
 * @returns
 */
export function setLoginTimestamp() {
  return Cookies.set(TimestampKey, Date.now());
}

/**
 * 获取登录时间戳
 * @returns
 */
export function getLoginTimestamp() {
  return Cookies.get(TimestampKey);
}

/**
 * 检查登录是否超时
 * @returns {boolean} true: 超时, false: 未超时
 */
export function checkLoginTimeout() {
  const loginTimestamp = getLoginTimestamp();
  const now = Date.now();
  return now - loginTimestamp > loginTimeout;
}
